*** NOTE:  This do-file performs the specifications in Table 11d with APL = pp_amount ***



program mymlogit_uh_lf
	version 10.1
	args lnf bxj1 bxj2 loc_j1_l1 loc_j1_l2 mass_point loc_j2_l1 loc_j2_l2
	tempvar denom_l1 denom_l2 mass_prob_l1 mass_prob_l2
	quietly generate double `denom_l1' = 1 + exp(`bxj1' + `loc_j1_l1') + exp(`bxj2' + `loc_j2_l1')
	quietly generate double `denom_l2' = 1 + exp(`bxj1' + `loc_j1_l2') + exp(`bxj2' + `loc_j2_l2')

	quietly generate double `mass_prob_l1' = exp(`mass_point') / (1 + exp(`mass_point'))
	quietly generate double `mass_prob_l2' = 1 / (1 + exp(`mass_point'))


	quietly replace `lnf' = ln((`mass_prob_l1'*(1/`denom_l1')) + (`mass_prob_l2'*(1/`denom_l2'))) if $ML_y1 == 0
	quietly replace `lnf' = ln((`mass_prob_l1'*(exp(`bxj1' + `loc_j1_l1')/`denom_l1')) + (`mass_prob_l2'*(exp(`bxj1' + `loc_j1_l2')/`denom_l2'))) if $ML_y1 == 1
	quietly replace `lnf' = ln((`mass_prob_l1'*(exp(`bxj2' + `loc_j2_l1')/`denom_l1')) + (`mass_prob_l2'*(exp(`bxj2' + `loc_j2_l2')/`denom_l2'))) if $ML_y1 == 2
end



log using "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Logs\20110231 APL regs -- original size -- refi_arm -- pp_amount.log", replace



***********************
***********************
*** APL regressions ***
*** refi_arm        ***
*** pp_amount       ***
***********************
***********************



use "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Data\apl law variables for stata.dta", clear
sort origmonth state
save "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Data\apl law variables for stata.dta", replace

use "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Data\All 10 MSAs\all10 10 size both refi_arm generated.dta", clear
keep if outsample_a != 1 & fico > 0 & cltv != 0 & balance != 0
compress
sort origmonth state
save "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Data\All 10 MSAs\TEMP all10 10 size both refi_arm generated.dta", replace
merge origmonth state using "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Data\apl law variables for stata.dta"

egen big_loan_num = group(loan_id)

drop loan_id pp_pen pp_term state mba_stat currmonth origmonth mba_stat_lead1 outcome_b outsample_b outcome_c hpa init_rate orig_risk_prem subprime loan_id_num flip_loantype anti_steering

generate apl_pre = pp_amount * prepay_pen
generate apl_pre_end = pp_amount * prepay_pen_end
*generate apl_doc = pp_amount * lownodoc
compress

generate msa_atl = 0
generate msa_bal = 0
generate msa_chi = 0
generate msa_los = 0
generate msa_mia = 0
generate msa_min = 0
generate msa_new = 0
generate msa_pho = 0
generate msa_pit = 0
generate msa_san = 0
replace msa_atl = 1 if msa_num == 1
replace msa_bal = 1 if msa_num == 2
replace msa_chi = 1 if msa_num == 3
replace msa_los = 1 if msa_num == 4
replace msa_mia = 1 if msa_num == 5
replace msa_min = 1 if msa_num == 6
replace msa_new = 1 if msa_num == 7
replace msa_pho = 1 if msa_num == 8
replace msa_pit = 1 if msa_num == 9
replace msa_san = 1 if msa_num == 10
compress

generate pre_atl = prepay_pen * msa_atl
generate pre_bal = prepay_pen * msa_bal
generate pre_chi = prepay_pen * msa_chi
generate pre_los = prepay_pen * msa_los
generate pre_mia = prepay_pen * msa_mia
generate pre_min = prepay_pen * msa_min
generate pre_new = prepay_pen * msa_new
generate pre_pho = prepay_pen * msa_pho
generate pre_pit = prepay_pen * msa_pit
generate pre_san = prepay_pen * msa_san
compress

generate pre_end_atl = prepay_pen_end * msa_atl
generate pre_end_bal = prepay_pen_end * msa_bal
generate pre_end_chi = prepay_pen_end * msa_chi
generate pre_end_los = prepay_pen_end * msa_los
generate pre_end_mia = prepay_pen_end * msa_mia
generate pre_end_min = prepay_pen_end * msa_min
generate pre_end_new = prepay_pen_end * msa_new
generate pre_end_pho = prepay_pen_end * msa_pho
generate pre_end_pit = prepay_pen_end * msa_pit
generate pre_end_san = prepay_pen_end * msa_san
compress

generate doc_atl = lownodoc * msa_atl
generate doc_bal = lownodoc * msa_bal
generate doc_chi = lownodoc * msa_chi
generate doc_los = lownodoc * msa_los
generate doc_mia = lownodoc * msa_mia
generate doc_min = lownodoc * msa_min
generate doc_new = lownodoc * msa_new
generate doc_pho = lownodoc * msa_pho
generate doc_pit = lownodoc * msa_pit
generate doc_san = lownodoc * msa_san
compress



*************************
*** APL variable only ***
*************************

*** Generating matrix of starting values using values from regression without APL variables ***
matrix starter = (0,-1.004546,0.4457879,0.4430964,-0.125912,-0.0100395,0.0279497,1.919051,0.6567576,0.359344,-0.4390689,0.1918609,-0.0028718,0.2114803,-0.0360978,0.0182741,-0.2611272,-0.3004807,-0.3795117,-0.4477868,-0.0491205,-0.3658061,-1.077389,-0.3577201,-0.3586125,-0.3623666,-0.4834658,-0.3209996,-0.3074168,-1.193395,-1.69003,0.8137426,0.2810087,0.4108736,0.3740258,0.8705097,0.5728092,-0.1976471,0.7967447,0.9458312,0.0269204,1.429778,0.5516938,0.9507313,1.004605,0.157052,0.8322278,0.4256433,-1.226601,0.1550935,-0.0565163,0.2265259,-0.2015247,0.2905041,0.2330446,0.1895982,0.0920318,0.1224566,0,-1.100449,0.664231,-0.051827,-0.0074018,-0.0026609,-0.0076085,1.978263,1.442228,0.2412723,-0.162803,0.1425351,-0.002898,0.217964,-0.2004921,0.0522303,-0.4721048,-0.323653,-0.5612758,-0.9668361,-1.128434,-0.0118225,-0.5903195,-0.0514663,-0.2691318,-1.242716,-0.0991785,-0.4656308,0.1205049,-0.9728896,-1.399942,0.3503488,0.2097193,-0.1172264,0.1727301,0.4845168,0.125286,-0.5368717,0.1199481,0.4264376,-0.0989632,1.216237,0.8929964,0.530838,0.8886807,0.3782948,0.2657153,0.5693475,-0.9487491,-0.08197,-0.0684539,0.2053763,-0.0932681,0.1427856,0.1367437,-0.0531878,0.2206672,0.1508975,-7.366381,1.240081,2.205109,-5.545813,1.296302)

*** Running mymlogit_uh_lf ***
ml model lf mymlogit_uh_lf (default: outcome_a = pp_amount prepay_pen prepay_pen_end lownodoc cashout fico cltv payment_adj2 adj_1st post_adj_1st spread ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_6molibor vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (payoff: pp_amount prepay_pen prepay_pen_end lownodoc cashout fico cltv payment_adj2 adj_1st post_adj_1st spread ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_6molibor vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (default_const1:) (default_const2:) (prob_coeff1:) (payoff_const1:) (payoff_const2:) if outsample_a != 1 & fico > 0 & cltv != 0 & balance != 0, cluster(big_loan_num)
*ml check
ml init starter, copy
ml maximize, iterate(25) difficult
outreg2 using "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic consistency\Stata\Stata Outregs\20110231 APL regs original size refi_arm pp_amount.txt", replace onecol text bracket(se) e(all)
*ml graph
do "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Do-files\mymlogit_uh_lf\Wald tests -- MSA-PLP interactions -- ARMs.do"

*** Saving the matrix of coefficient estimates ***
matrix a = e(b)

*** Creating starter matrices with 0 values for the APL*PLP interaction term coefficient estimates ***
*** -- This uses e(df_m), which is the number of explanatory variables in each equation, not       ***
***    including constant terms.                                                                   ***
*** -- For prepay_pen regressions, the APL variable is interacted with both prepay_pen and         ***
***    prepay_pen_end, requiring an extra pair of APL*PLP interaction term coefficient estimates.  ***
matrix afirst = a[1,1]
matrix asecond = a[1,2..(e(df_m)+1)]
matrix athird = a[1,(e(df_m)+2)..((e(df_m)*2)+5)]
matrix starter1 = (afirst,0,0,asecond,0,0,athird)
*matrix starter2 = (afirst,0,asecond,0,athird)



*********************************************
*** APL*prepay_pen and APL*prepay_pen_end ***
*********************************************

*** Running mymlogit_uh_lf ***
ml model lf mymlogit_uh_lf (default: outcome_a = pp_amount apl_pre apl_pre_end prepay_pen prepay_pen_end lownodoc cashout fico cltv payment_adj2 adj_1st post_adj_1st spread ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_6molibor vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (payoff: pp_amount apl_pre apl_pre_end prepay_pen prepay_pen_end lownodoc cashout fico cltv payment_adj2 adj_1st post_adj_1st spread ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_6molibor vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (default_const1:) (default_const2:) (prob_coeff1:) (payoff_const1:) (payoff_const2:) if outsample_a != 1 & fico > 0 & cltv != 0 & balance != 0, cluster(big_loan_num)
*ml check
ml init starter1, copy
ml maximize, iterate(25) difficult
outreg2 using "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic consistency\Stata\Stata Outregs\20110231 APL regs original size refi_arm pp_amount.txt", append onecol text bracket(se) e(all)
*ml graph
do "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Do-files\mymlogit_uh_lf\Wald tests -- MSA-PLP interactions -- ARMs.do"

/*

********************
*** APL*lownodoc ***
********************

*** Running mymlogit_uh_lf ***
ml model lf mymlogit_uh_lf (default: outcome_a = pp_amount apl_doc prepay_pen prepay_pen_end lownodoc cashout fico cltv payment_adj2 adj_1st post_adj_1st spread ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_6molibor vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (payoff: pp_amount apl_doc prepay_pen prepay_pen_end lownodoc cashout fico cltv payment_adj2 adj_1st post_adj_1st spread ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_6molibor vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (default_const1:) (default_const2:) (prob_coeff1:) (payoff_const1:) (payoff_const2:) if outsample_a != 1 & fico > 0 & cltv != 0 & balance != 0, cluster(big_loan_num)
*ml check
ml init starter2, copy
ml maximize, iterate(25) difficult
outreg2 using "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic consistency\Stata\Stata Outregs\20110231 APL regs original size refi_arm pp_amount.txt", append onecol text bracket(se) e(all)
*ml graph
do "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Do-files\mymlogit_uh_lf\Wald tests -- MSA-PLP interactions -- ARMs.do"

*/

log close



program drop mymlogit_uh_lf



clear



